<?php
namespace App\Http\Controllers\Admin;

use App\Service\Admin\OrderService;
use Illuminate\Http\Request;

class OrderController extends BaseController
{
    //首页

    public function __construct()
    {
    }

    //订单列表
    public function getIndex(Request $request)
    {
        $pas['keyword'] = $request->post('keyword', '');
        $pas['phone'] = $request->post('phone', '');
        $pas['status'] = $request->post('status', 0);
        $pas['start_day'] = $request->post('start_day', '');
        $pas['end_day'] = $request->post('end_day', '');
        $pas['delivery'] = $request->post('delivery', '');
        $pas['pay_way'] = $request->post('pay_way', '');
        $pages['start'] = $request->post('start', 1);
        $pages['limit'] = $request->post('limit', 10);
        $res = (new OrderService())->getIndex($pas, $pages);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //订单详情
    public function getOne(Request $request)
    {
        $id = $request->post('id', 0);
        $res = (new OrderService())->getOne($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //订单审核
    public function setAuth(Request $request)
    {
        $id = $request->post('id', 0);
        $status = $request->post('status', 0);
        if (!$id) {
            return $this->toJson(-2, '订单ID必须的');
        }
        if (!in_array($status, [1, 2])) {
            return $this->toJson(-2, '订单状态错误');
        }
        $res = (new OrderService())->setAuth($id, $status);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        //通过审核，发起退款
        if ($status == 2) {
            $uid = $res['data']['uid'];
            $orderno = $res['data']['orderno'];
            $obj = new \App\Http\Controllers\ApiXcx\WxPayController();
            $obj->refundByAuth($uid, $orderno);
        }
        return $this->toJson();
    }

    //订单发货
    public function send(Request $request)
    {
        $id = $request->post('id', 0);
        $pas['wuliu'] = $request->post('wuliu', '');
        $pas['wuliuhao'] = $request->post('wuliuhao', '');
        if (!$id) {
            return $this->toJson(-2, '订单ID必须的');
        }
        if (!$pas['wuliu']) {
            return $this->toJson(-2, '快递公司必须的');
        }
        if (!$pas['wuliuhao']) {
            return $this->toJson(-2, '快递单号必须的');
        }
        $res = (new OrderService())->send($id, $pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //批量发货
    public function batchSend(Request $request)
    {
        $os = $request->post('os', '');
        if (!$os) {
            return $this->toJson(-2, '订单必须的');
        }
        $res = (new OrderService())->batchSend($os);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //修改备注
    public function updateRemark(Request $request){
        $id = $request->post('id', 0);
        $remark = $request->post('remark', '');
        if (!$id) {
            return $this->toJson(-2, '订单ID必须的');
        }
        $res = (new OrderService())->updateRemark($id, $remark);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //退货审核
    public function tuiAuth(Request $request)
    {
        $id = $request->post('id', 0);
        if (!$id) {
            return $this->toJson(-2, '订单ID必须的');
        }
        $res = (new OrderService())->tuiAuth($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //导出功能，被废弃
    public function getOut(Request $request)
    {
        $pas['keyword'] = $request->post('keyword', '');
        $pas['phone'] = $request->post('phone', '');
        $pas['status'] = $request->post('status', 0);
        $res =(new OrderService())->getOlist($pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        $res = $res['data'];
        $titles = ['订单号', '下单用户', '手机号', '商品名称', '商品图片', 'SKU名称', '单价(元)', '数量', '实付款(元)', '收件人', '收件人地址',];
        $fname = 'daochu/scorder-'.time().'.txt';
        file_put_contents($fname, implode('<-->', $titles).PHP_EOL, FILE_APPEND);
        foreach ($res as $v) {
            $arys = [
                $v['orderno'], $v['uname'], $v['phone'], $v['gname'],
                $v['gimg'], $v['sku_name'], $v['price'], $v['num'],
                $v['real_pay'], $v['accepter'], $v['acceptDoor'],
            ];
            file_put_contents($fname, implode('<-->', $arys).PHP_EOL, FILE_APPEND);
        }
        $fpath = env('DOMAIN').'/'.$fname;
        return $this->toJson(0, '', $fpath);
    }
}